/**
 * Created by lixiaodi on 2019/04/18.
 */
mini.parse();
var form = new mini.Form("form1");  //得到页面上的表单
var data1;
var personCode = mini.get('uCode');
var user_phone=mini.getByName("user_phone");
var checkBox1 = document.getElementById('checkbox1');
var checkBox2 = document.getElementById('checkbox2');
var checkBox3 = document.getElementById('checkbox3');
console.log(checkBox1.checked);
console.log(checkBox2.checked);
console.log(checkBox3.checked);

//页面初始化
$(function(){
	 
})
//人员参照
function changePerson(){
	console.log('人员选择');
	var btnEdit = this;
	mini.open({
        url: bootPATH + "../frame/frame1/pages/alertBox/personReferenceWin.html",
        title: "人员档案",
        width: 600,
        height: 400,
        ondestroy: function (action) {
            //if (action == "close") return false;
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);    //必须
                var personInfo = data[0];
                //得到form表单的值
                var formObj = form.getData();
                //赋值
                formObj.uName = personInfo.perName;
                formObj.userCode = personInfo.perCode;
                formObj.user_phone = personInfo.perPhone;
                formObj.user_email = personInfo.email;
                if(personInfo.company){
                	formObj.user_company = personInfo.company.companyName;
                }
                if(personInfo.company){
                	formObj.user_department = personInfo.department.departmentName;
                }
                userCodeInput.setValue(personInfo.perCode)
                userCodeInput.setText(personInfo.perCode)
                console.log(formObj,'用户信息');
                form.setData(formObj);
            }
        }
    });
}

//保存
function SaveData() {
    var formiddd=localStorage.getItem("formidd");
    formiddd = parseInt(formiddd);
	var mangeId = data1.mangeId;
	console.log("-=============")
	console.log(mangeId);
    var pid;
    var formId = formiddd;
	var remarks = data1.ids.remarks;
	console.log(data1.assignee);
	if (data1.assignee!=null&&data1.assignee!=undefined){
        pid=data1.assignee[0].PersonnelFiles_id;
    }else{

    }
	if(mangeId){
        console.log(formiddd);
  	 $.ajax({
   	        url: ajaxUrl + "/activitiTaskController/audit_bz.do?taskId="+mangeId+"&comment="+remarks+"&pid="+pid+"&fromId=" +formId,
  	        type: 'POST',

   	        contentType : "application/json",
   	        success: function (data) {
   	        	console.log(data);
   	        	if(data.type == 'success'){
   	        		mini.alert('操作成功');
   	        		if(checkBox1.checked){
                        var msg2 = '消息有新任务|' + userInfoObj.userId+'|'+data.userInfo.userId;
                         console.log(msg2);
                         var msg3 = '您有一条待办任务消息提醒:'+user_phone.getValue()+'|'+ userInfoObj.userId+'|'+data.userInfo.userId;
   	        			console.log(msg3);
                         if(checkBox2.checked == true && checkBox3.checked == true){
   	        				send(msg2);//给发起人发送提醒
   	        				send(msg3);//给下一节点人发送提醒
   	        			}else if(checkBox2.checked == true && checkBox3.checked == false) {
                            send(msg2);//给发起人发送提醒

   	        			}else if(checkBox2.checked == false && checkBox3.checked == true){
   	        			 	send(msg3); //给下一节点人发送提醒
   	        			    }
   	        		}

   	        	}else{
   	        		mini.alert('操作失败');
   	            	
   	        	}

   	            //CloseWindow("save");
   	        },
   	        error: function (data) {
   	        	mini.alert('失败');
   	            //CloseWindow();
   	        }
   	    });
   }else{
   	mini.alert('无需要批准的单据');
   }
   

}

//标准方法接口定义（编辑）
function SetData(data) {
	data1 = data;
	console.log(data1,'传值');
    if (data1.action == "approval") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data1 = mini.clone(data1);
        console.log(data1,'跳转页面时的数据')
        if (data1.assignee.length > 0){
            var assigneeData = data1.assignee[0];
            if(assigneeData){
                personCode.setValue(assigneeData.PersonnelFiles_name);
                personCode.setText(assigneeData.PersonnelFiles_name);
            }
        }else {
            personCode.setValue('结束');
            personCode.setText('结束');
        }
    }
}
//父页面传值
function GetData() {
    var o = form.getData();
    return o;
}

//当选择角色改变时
function onUserChanged(e) {
    var roleIdValue = roleLists.getValue();
}

//重置
function onReset(){
    var ids = form.getData();
    var userId = ids.userId;
    var resetBtn = mini.get("resetBtn");
    var passwords = mini.get("password");
    passwords.allowInput="false";
    $.ajax({
        url: ajaxUrl + "/user/replacePassword.do?userId=" + userId,
        type:"post",
        success: function (data) {
            resetBtn.enabled="true";
        }
    });

}
//密码可见
function seeFun(e){
	var pwInput = $('#password1')[0];
	console.log(pwInput);
	console.log(pwInput.class);
}
//校验账号名称是否重复
function findByUserName(){
	var ids = form.getData();
	var userName = ids.userName;
	
	$.ajax({
		type 	: 'POST',
		url  	: ajaxUrl + '/user/findUserName.do',
		data 	: {
			userName : userName
		},
		success : function(data){
			if(data.state == 'error'){
				mini.alert(data.message);
				mini.getByName('userName').setValue(''); 
			}
		},
		error   : function(message) {
			mini.alert(message);
		}
	})
}
//校验模块必输
function onNotSpaceValidation(e) {
    if (e.isValid) {
        if (!e.value) {
            e.errorText = "不能为空";
            e.isValid = false;
        }
    }
}
//保存时确认密码是否和密码一致
function onConfirmValidation(e){
	var passwordTxt1 = passwordEl.getValue();
	if (e.isValid) {
		if(e.value != passwordTxt1){
			e.errorText = '确认密码和密码要保持一致';
            e.isValid = false;
		}
    }
}
//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
//保存ajax
function ajaxSave(ids){
	var userInfo = {
            userId : ids.userId,
            userCode: ids.userCode,
            userName : ids.userName,
            personCode : ids.personCode,
            uName : ids.uName,
            password: ids.password,
            roleId : ids.role.roleId,
            userState : ids.userState,
            loginDate : ids.loginDate,
            createDate : ids.createDate
        };
        console.log(userInfo,'保存');
        userInfo = JSON.stringify(userInfo);
        $.ajax({
            url: ajaxUrl + "/user/saveUser.do",
            type: 'POST',
            data: userInfo,
            contentType : "application/json",
            success: function (data) {
                CloseWindow("save");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(jqXHR.responseText);
                CloseWindow();
            }
        });
}


//是否发送提醒消息
function toggleFieldSet(ck, id) {
    var dom = document.getElementById(id);
    dom.className = !ck.checked ? "hideFieldset" : "";

}